package com.booking.action;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.booking.bean.Foodorder;
import com.booking.service.OrderService;
import com.booking.service.UserService;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
public class OrderAction extends ActionSupport implements ModelDriven<Foodorder>{

	@Autowired
	private Foodorder foodorder = new Foodorder();
	
	@Autowired
	private OrderService service;
	
	
	@Override
	public Foodorder getModel() {
		return foodorder;
	}
	private int status ;
	
	public void setStatus(int status) {
		this.status = status;
	}
	private int statu ;
	
	public void setStatu(int statu) {
		this.statu = statu;
	}
	
	public String submitOrder() throws Exception{
		//String regeditError;
		
		//得到购物车的商品信息
		Foodorder forder = (Foodorder) ActionContext.getContext().getSession().get("forder");
		
		//BeanUtils.copyProperties(forder, foodorder);
		foodorder.setTotalmount(forder.getTotalmount());
		foodorder.setShop(forder.getShop());
		foodorder.setSellerid(forder.getShop().get(0).getFood().getSellerid());
		//forder.setTotalmount(orderService.cluTotal(forder));
		
		String order = service.submitOrder(foodorder);
		
		ActionContext.getContext().getSession().remove("forder");
	/*	if(order == null || order.equals("")){
			//提交订单失败
			ActionContext.getContext().put("orderError", "提交订单失败");			
			return "input";
		}else{
			//提交订单成功
			ActionContext.getContext().put("orderError", "提交订单成功");			
			return "query";
		}*/
		ActionContext.getContext().getSession().put("sta",0);

		queryNoneOrder();
		
		return "success";

	}
	public String queryNoneOrder(){
		
		if(status == 0){
			List<Foodorder> noneOrder = service.queryOrder(status,foodorder.getUserid());
			ActionContext.getContext().getSession().put("sta",0);
			ActionContext.getContext().getSession().put("noneOrder",noneOrder);
		}
		
		return "query";
		
	}
	public String queryDoneOrder(){
		
		 if(statu ==1){
			List<Foodorder> doneOrder = service.queryOrder(statu,foodorder.getUserid());
			ActionContext.getContext().getSession().put("sta",1);
			ActionContext.getContext().getSession().put("doneOrder",doneOrder);
		}
		
		return "query";
		
	}
	public String deleteOrder(){
//		Foodorder forder = (Foodorder) ActionContext.getContext().getSession().get("noneOrder");
		
		 service.DeleteOrder(foodorder.getId());
		if(foodorder.getStatus()==0){
			ActionContext.getContext().getSession().put("sta",0);
		}
		if(foodorder.getStatus()==1){
			ActionContext.getContext().getSession().put("sta",1);
		}
/*		ActionContext.getContext().getSession().put("noneOrder",noneOrder);
*/		
		return "query";
		
	}

}
